/**
*	插件：上传文件中心
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-25
*/

js.rockfile = {
	uploadurlbase:'',
	uploadurlbasess:'',
	cnum:'',
	url:'',
	key:'',
	data:{},
	init:function(){
		var infostr = js.getoption('rockfile');
		rockfilecallback = function(d){js.rockfile.callback(d);}
		if(infostr){
			var info 	= js.decode(infostr);
			var myinfo 	= js.getoption('userinfo');
			this.url 	= info.url;
			this.key 	= info.key;
			var url 	= info.url+'?m=filejs&key='+info.key+'&a=base&callback=rockfilecallback';
			var urls 	= info.url+'?m=filejs&key='+info.key+'&callback=rockfilecallback';
			if(myinfo){
				var arr 	= js.decode(myinfo);
				url+='&optid='+arr.id+'&optname='+jm.base64encode(arr.name)+'';
			}
			this.uploadurlbase 	 = url;
			this.uploadurlbasess = urls;
		}
	},
	create:function(fun){
		if(!js.rockfile.uploadurlbase)return;
		js.importjs(js.rockfile.uploadurlbase, fun);
	},
	get:function(){
		return this.uploadurlbase;
	},
	importjs:function(fun){
		if(!this.uploadurlbase)return js.show.error('未设置文件上传中心');
		js.importjs(this.uploadurlbase, fun);
	},
	importbase:function(fun){
		if(!js.rockfile.uploadurlbasess)return;
		js.importjs(js.rockfile.uploadurlbasess, fun);
	},
	callback:function(d){
		d.fileid = d.id;
		this.data[d.filenum] = d;
		js.ajax.post({
			url:'api/file/save',
			cnum:this.cnum,
			data:d
		},function(ret){
			
		});
	},
	changeface:function(cans,lxb){
		if(!js.updateface && !lxb){
			js.rockfile.importjs(function(){
				js.rockfile.changeface(cans, true);
			});
			return;
		}
		if(!js.updateface){js.show.error('未设置上传文件中心');return;}
		js.rockfile.cnum = '';
		if(cans.cnum)js.rockfile.cnum = cans.cnum;
		js.updateface(cans.name, cans.fields, function(lus){
			var str = lus.replace('{FILEURL}',FILE_URL);
			if(cans.fields && get(cans.fields))get(cans.fields).value=str;
			if(cans.oncallback)cans.oncallback(str);
		});
	},
	fileuppath:function(lj){
		if(lj)lj = lj.replace('{FILEURL}', this.url);
		if(lj && lj.indexOf('http')==-1)lj = this.url+lj;
		return lj;
	},
	uploafile:function(cans,lxb){
		if(!js.showuploadfile && !lxb){
			js.rockfile.importjs(function(){
				js.rockfile.uploafile(cans, true);
			});
			return;
		}
		if(!js.showuploadfile){js.show.error('未设置上传文件中心');return;}
		ismobile = (js.web.ismobile() && winWb()<400) ? 1 :0 ;
		js.rockfile.cnum = '';
		if(cans.cnum)js.rockfile.cnum = cans.cnum;
		js.showuploadfile(cans.name,cans.uptype, function(fnum){
			if(fnum)js.rockfile.uploafileback(fnum, cans);
		});
	},
	uploafileback:function(fnum, cans){
		var farr = [],arr = fnum.split(','),d,fileids='',sval='';
		if(cans.inputobj)sval = cans.inputobj.value;
		for(var i=0;i<arr.length;i++){
			d = this.data[arr[i]];
			if(d){
				if(d.thumbpath)d.thumbpath = this.fileuppath(d.thumbpath);
				if(fileids)fileids+=',';
				fileids+=d.id;
				farr.push(d);
			}
		}
		if(sval){
			if(fileids)fileids+=',';
			fileids+=sval;
		}
		if(cans.inputobj)cans.inputobj.value = fileids;
		if(cans.view)this.fileviewshow(fnum, cans.view, true);
		if(cans.oncallback)cans.oncallback(farr);
	},
	
	view:function(fnum,lx){
		js.ajax.get({
			url:'api/file/data',
			loading:'加载中...',
			data:{fnum:fnum,lx:lx}
		},function(ret){
			js.rockfile.viewback(ret.data);
		});
	},
	viewback:function(da){
		if(da.lx==0 || da.lx==2){
			js.open(da.url, 800, 500);
		}
		if(da.lx==1)js.location(da.url);
	},
	
	//展示处理
	setfiledata:function(da){
		var i,len 	 = da.length;
		for(i=0;i<len;i++)this.data[da[i].fileid] = da[i];
	},
	fileviewshow:function(fids, view, isdel){
		var s = '',fida = fids.split(','),d;
		for(var i=0;i<fida.length;i++){
			d = this.data[fida[i]];
			if(d)s+= this.fileviewstr(d,isdel);
		}
		$('#'+view+'').prepend(s);
	},
	fileviewstr:function(d,isdel){
		this.data[d.fileid] = d;
		var src = ''+FILE_URL+'images/fileicons/'+js.filelxext(d.fileext)+'.gif';
		var clds= '',s='';
		if(d.thumbpath){
			src = js.fileuppath(d.thumbpath);
			s+= '<span>';
			s+= '<img src="'+src+'" class="cursor" onclick="js.viewimg(this)" height="30" width="30">';
			if(isdel)s+= '&nbsp;<a onclick="js.rockfile.del(\''+d.fileid+'\', this)" class="zhu" href="javascript:;">删</a>';
			s+= '&nbsp;';
			s+= '</span>';
		}else{
			s = '<div style="padding-bottom:5px">';
			s+= '<img src="'+src+'" align="absmiddle"> '+d.filename+'<span class="hui fontsize14">('+d.filesizecn+')</span>';
			s+= '&nbsp;<a onclick="js.rockfile.view(\''+d.filenum+'\',0)" class="zhu noprint" href="javascript:;">预览</a>';
			if(!d.notdown)s+= '&nbsp;<a onclick="js.rockfile.view(\''+d.filenum+'\',1)" class="zhu noprint" href="javascript:;">下载</a>';
			if(isdel)s+= '&nbsp;<a onclick="js.rockfile.del(\''+d.fileid+'\',this)" class="zhu" href="javascript:;">删</a>';
			s+= '</div>';
		}
		return s;
	},
	del:function(id,o,ob){
		var d = this.data[id];
		if(!ob){
			js.modal.confirm({
				msg:'确定要删除文件('+d.filename+','+d.filesizecn+')吗？'
			}, function(jg){
				if(jg=='yes')js.rockfile.del(id,o,true);
			});
			return;
		}
		var view = $(o).parent().parent().attr('id');
		$(o).parent().remove();
		this.data[id] = false;
		if(view){
			var input = get(''+view+'_input');
			if(input){
				var val = input.value,str='';
				if(val){
					var arr = val.split(',');
					for(var i=0;i<arr.length;i++){
						if(this.data[arr[i]]){
							if(str)str+=',';
							str+=arr[i];
						}
					}
				}
				input.value = str;
			}
		}
		js.ajax.get({
			url:'api/file/del',
			cnum:this.cnum,
			data:{fnum:d.filenum}
		});
	}
}